// pages/tixian/tixian.js
const http = require("../../utils/util.js");
var Crypto = require('../../utils/cryptojs/cryptojs.js').Crypto;
var app = getApp();
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    count:10000,
    money:'',
    options:[],
    balance:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
  //  if(options.type=="bar"){
  //    wx.navigateTo({
  //      url: './yue',
  //    })
  //  }

console.log(options)
    that.setData({
      options:options
    })

  },
  quanbuTixian:function(){
    var that  =  this;
    console.log("全部体现")
    that.setData({
      money:parseFloat(that.data.balance.balance/100),
    })
  },
  getInputValue:function(e){
    var that = this;
    console.log(e.detail.value)
    that.setData({
      money:e.detail.value,
    })
  },
  refound:function(){
    var that = this;
    console.log("调用提现")
    console.log(that.data.money)
    console.log(that.data.money)
    if (that.data.money < 1.5) {
      app.showInfo("提现金额不能小于1.5，请重新输入")
    } else {
      wx.getStorage({
        key: 'token',
        success: function(res) {
          var token = res.data;
          var url = app.globalData.localApi + "/user/refund";
          var timestamp = app.timestamp();
          wx.request({
            url: url,
            method: "POST",
            data: {
              token: token,
              timestamp: timestamp,
              sign: Crypto.MD5(app.sign(url, token, timestamp)),
              refund: parseFloat(that.data.money * 100),
              url:app.urlStr(url),
            },
            success: function (res) {
              console.log(res)
              if(res.data.code==201){
                app.login();
              }else if(res.data.success){
                app.showInfo("提现成功")
                wx.navigateBack({
                  delta:1,
                })
              }else {
                app.showInfo(res.data.msg)
              }
            }
          })
        },
     
    })
    }
    
  },
  // 请求余额
  balance: function () {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        var token = res.data;
        var timestamp = app.timestamp();
        var url = app.globalData.localApi + "/user/balance";
        var data = {
          timestamp: timestamp,
          token: token,
          sign: Crypto.MD5(app.sign(url, token, timestamp)),
          url:app.urlStr(url),
        };
        wx.request({
          url: url,
          method: "POST",
          data: data,
          success: function (res) {
            console.log(res.data)
            if (res.data.code == 201) {
              app.login();
            }

            that.setData({
              balance: res.data.data,
            })
          },
        })
      },
    })
  },
  // 表单提交；
  fromSubmit: function (e) {
    var that = this;
    var formId = e.detail.formId
    app.formId(formId);
    that.setData({
      formId: formId,
    })
    that.refound();
  },  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    console.log("zhixing onSHow")
    wx.switchTab({
      url: 'tixian',
    })
    that.balance();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})